package com.dzx.swing;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author dzx
 * @ClassName:
 * @Description:
 * @date 2019年07月25日 09:25:39
 */
public class SwingLoginExample {
    public static void main(String[] args) {
        //创建JFrame 实例
        JFrame jFrame = new JFrame("Login Example");

        jFrame.setSize(350, 200);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /**
         * 创建面板，这个类似于HTML的div标签 我们
         * 可以创建多个面板并在jframe中指定位置
         * 面板中我们可以添加文本字段，按钮及其他组件
         */
        JPanel jPanel = new JPanel();

        jFrame.add(jPanel);

        placeComponents(jPanel);

        jFrame.setVisible(true);
    }

    private static void placeComponents(JPanel jPanel) {


        /**
         * 布局设置为null
         */
        jPanel.setLayout(null);

        /**
         * 创建jlabel
         */
        JLabel userLabel = new JLabel("User:");
        userLabel.setBounds(10, 20, 80, 25);
        jPanel.add(userLabel);

        final JTextField userText = new JTextField(20);
        userText.setBounds(100, 20, 165, 25);
        jPanel.add(userText);

        JLabel passwordLabel = new JLabel("Password:");
        passwordLabel.setBounds(10, 50, 80, 25);
        jPanel.add(passwordLabel);

        final JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(100, 50, 165, 25);
        jPanel.add(passwordText);

        JButton jButton = new JButton("Login");
        jButton.setBounds(10, 80, 80, 25);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("用户名：" + userText.getText() + ",密码：" + passwordText.getText());
                System.out.println("正在验证...请稍后");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                System.out.println("登录成功");
            }
        });
        jPanel.add(jButton);
    }
}
